﻿var emptyData={total:0,rows:[]};
var tabselected=0;
var formTypeData2=[{code:'0',name:'水费'},{code:'1',name:'电费'}];
var formTypeData=[{code:'0',name:'公摊'},{code:'1',name:'住宅'},{code:'2',name:'门面'},{code:'3',name:'其它'}];

//初始化入口
$(function() {

    $('#linkbindroom').show();
    $('#linkbindshop').hide();
            
    $('#cbarea').combobox({ url: '../Controller.aspx?controller=SysInitController&method=GetAreaList' });
    $('#cbbuild').combobox({ url: '../Controller.aspx?controller=SysInitController&method=GetBuildList&areacode=01' });

    $('#cbarea').combobox('setValue', '01');
    $('#cbbuild').combobox('setValue', '01');

    $('#cbarea').combobox('options').onSelect = function(record) {
        $('#cbbuild').combobox('options').url = "../Controller.aspx?controller=RoomBrowseController&method=GetBuildList&areacode=" + record.Code;
        $('#cbbuild').combobox('reload');
    }
    
    $('#roomlistGird').datagrid('options').onSelect=function(rowIndex, rowData){
        $('#itemlistGrid').datagrid('options').url="../Controller.aspx?controller=SysInitController&method=GetItemRelationList&srccId=" + rowData.RoomId;
        $('#itemlistGrid').datagrid('reload');
        
        $('#meterlistGrid').datagrid('options').url="../Controller.aspx?controller=SysInitController&method=GetSrccMakerList&srccId="+rowData.RoomId;
        $('#meterlistGrid').datagrid('reload');
    }
    
    $('#shoplistGrid').datagrid('options').onSelect=function(rowIndex, rowData){
        $('#itemlistGrid').datagrid('options').url="../Controller.aspx?controller=SysInitController&method=GetItemRelationList&srccId=" + rowData.ShopId;
        $('#itemlistGrid').datagrid('reload');
        
        $('#meterlistGrid').datagrid('options').url="../Controller.aspx?controller=SysInitController&method=GetSrccMakerList&srccId="+rowData.ShopId;
        $('#meterlistGrid').datagrid('reload');
    }
    //选择项目的时候触发编辑
    $('#itemlistGrid').datagrid('options').onSelect=function(rowIndex, rowData){
        var rows= $('#itemlistGrid').datagrid('getRows');
        for(var i=0;i<rows.length;i++){
            $('#itemlistGrid').datagrid('endEdit', i);
        }
        if(parseInt(rowData.InitFlag)==0){
            $('#itemlistGrid').datagrid('beginEdit', rowIndex);
        }
    }
    //选择用表触发编辑初始化读数
    $('#meterlistGrid').datagrid('options').onSelect=function(rowIndex, rowData){
        var rows= $('#meterlistGrid').datagrid('getRows');
        for(var i=0;i<rows.length;i++){
            $('#meterlistGrid').datagrid('endEdit', i);
        }
        $('#meterlistGrid').datagrid('beginEdit', rowIndex);
    }
    //点击左边Tab时记录值
    $('#tabIndex').tabs('options').onSelect=function(title){
        if(title=="房间"){
            tabselected=0;
            $('#linkbindroom').show();
            $('#linkbindshop').hide();
        }else{
            tabselected=1;
            $('#linkbindroom').hide();
            $('#linkbindshop').show();
        }
    }
    
    $('#cbformType2').combobox('loadData',formTypeData2);
    $('#cbformType').combobox('loadData',formTypeData);
    
    $('#cbsformType2').combobox('loadData',formTypeData2);
    $('#cbsformType').combobox('loadData',formTypeData);
    
    $('#cbmeteritemcode').combobox('setValue','');

    $('#dlg-bindingitem').dialog('close');
    $('#dlg-bingingmeter').dialog('close');
    $('#dlg-metersearch').dialog('close');
});


function btn_roomsearch() {
    var areacode = $('#cbarea').combobox('getValue');
    var buildcode = $('#cbbuild').combobox('getValue');
    var roomname = $('#txtroom').val();
    $('#roomlistGird').datagrid('options').url = '../Controller.aspx?controller=SysInitController&method=GetRoomList&areacode=' + areacode + '&buildcode=' + buildcode + '&roomname=' + roomname;
    $('#roomlistGird').datagrid('reload');
}

function btn_shopsearch() {
    var shopname = $('#txtshop').val();

    $('#shoplistGrid').datagrid('options').url = '../Controller.aspx?controller=SysInitController&method=GetShopList&shopname=' + shopname;
    $('#shoplistGrid').datagrid('reload');
}

function dlg_bingingitem_roomshow() {
    $('#dlg-bindingitem').dialog({ title: '所有住宅收费项目批量绑定' });
    $('#bindtype').val('0');
}

function dlg_bingingitem_shopshow() {
    $('#dlg-bindingitem').dialog({ title: '所有门面收费项目批量绑定' });
    $('#bindtype').val('1');
}

function dlg_bingingitem_save(){
    var rows= $('#itemlistGrid').datagrid('getRows');
    for(var i=0;i<rows.length;i++){
        $('#itemlistGrid').datagrid('endEdit', i);
    }
        
    rows = $('#itemlistGrid').datagrid('getChanges');
    if(rows.length>0){
        var arr=[];
        for(var i in rows)arr.push(rows[i].SrccId+'#'+rows[i].ItemCode+'#'+rows[i].Lastmaturity+'#'+rows[i].Thismaturity+'#'+rows[i].Lastsurplus+'#'+rows[i].LastLateFee);
        //alert(arr.join(','));
        requestAjax('../Controller.aspx?controller=SysInitController&method=UpdateBindingItem', { data: arr.join(',')}, function(data) {
            $('#itemlistGrid').datagrid('reload');
        });
    }
}

function dlg_bingingitem_addshow() {
    if (tabselected==0) {
        var selected = $('#roomlistGird').datagrid('getSelected');
        if (selected) {
            $('#dlg-bindingitem').dialog({ title: '添加收费项目绑定' });
            $('#bindtype').val('2');
            $('#binditemId').val(selected.RoomId);
            $('#ownerId').val(selected.OwnerId);
        }
    } else {
        var selected = $('#shoplistGrid').datagrid('getSelected');
        if (selected) {
            $('#dlg-bindingitem').dialog({ title: '添加收费项目绑定' });
            $('#bindtype').val('2');
            $('#binditemId').val(selected.ShopId);
            $('#ownerId').val(selected.OwnerId);
        }
    }
}

function dlg_bingingitem_del() {
    var selected=$('#itemlistGrid').datagrid('getSelected');
    if(selected){
            $.messager.confirm('询问', '是否要删除此收费项目？', function(r){
		        if (r){
                    requestAjax('../Controller.aspx?controller=SysInitController&method=DeleteBindingItem', { srccId: selected.SrccId,itemcode:selected.ItemCode}, function(data) {
                        $('#itemlistGrid').datagrid('reload');
                    });
                }
			});
    }
}

function dlg_bindingitem_confirm() {
    var type = $('#bindtype').val();
    var binditemId = $('#binditemId').val();
    var ownerId = $('#ownerId').val();
    var delitem;
    var itemcode='';
    if ($("#ckdelitem").attr("checked"))
        delitem = 1;
    else
        delitem = 0;

    var selected = $('#serviceitemGird').datagrid('getSelections');
    if (selected.length > 0) {
    
        for (var i = 0; i < selected.length; i++) {
            if (itemcode == '') itemcode = selected[i].Code;
            else itemcode = itemcode + "," + selected[i].Code;
        }
        $.messager.progress({
            title: '批量绑定收费项目',
            msg: '正在操作中请等待...'
        });
        requestAjax('../Controller.aspx?controller=SysInitController&method=BatchBindingItem', { type: type, binditemId: binditemId, ownerId: ownerId, delitem: delitem, itemcode: itemcode }, function(data) {
            $.messager.progress('close');
            $('#dlg-bindingitem').dialog('close');
            $('#itemlistGrid').datagrid('reload');
        }, function() { $.messager.progress('close'); });

    }
}

function dlg_bindingmeter_show(){
    $('#dlg-bingingmeter').dialog({title:'用表绑定给业主-批量'});
    $('#metertype').val('0');
    $('#cbformType').combobox('enable');
}

function dlg_bingingmeter_confirm(){
    if(formvalidate('dlg-bingingmeter')==false)return false;
    var formtype=$('#cbformType').combobox('getValue');
    var formtype2=$('#cbformType2').combobox('getValue');
    var metercount=$('#txtmeternum').val();
    var itemcode=$('#cbmeteritemcode').combobox('getValue');
    var type=$('#metertype').val();
    var ownerId=$('#meterownerId').val();
    var ownerName=$('#meterownerName').val();
    var srccId=$('#metersrccId').val();
    var srccName=$('#metersrccName').val();
           
    var param={type:type,ownerId:ownerId,ownerName:ownerName,srccId:srccId,srccName:srccName, formtype: formtype, formtype2: formtype2, metercount: metercount, itemcode: itemcode}; 
    $.messager.progress({
            title: '批量生成用表',
            msg: '正在操作中请等待...'
        });
    requestAjax('../Controller.aspx?controller=SysInitController&method=BatchBingingMeter', param, function(data) {
            $.messager.progress('close');
            $('#dlg-bingingmeter').dialog('close');
            $('#meterlistGrid').datagrid('reload');
        }, function() { $.messager.progress('close'); });
}



function btn_metersave(){
    var rows= $('#meterlistGrid').datagrid('getRows');
    for(var i=0;i<rows.length;i++){
        $('#meterlistGrid').datagrid('endEdit', i);
    }
        
    rows = $('#meterlistGrid').datagrid('getChanges');
    if(rows.length>0){
        var arr=[];
        for(var i in rows)arr.push(rows[i].MeterId+'#'+rows[i].InitMeterNum);
        //alert(arr.join(','));
        requestAjax('../Controller.aspx?controller=SysInitController&method=UpdateMeter', { data: arr.join(',')}, function(data) {
            $('#meterlistGrid').datagrid('reload');
        });
    }
}

function btn_meteradd(){
    
    if (tabselected==0) {
        var selected = $('#roomlistGird').datagrid('getSelected');
        if (selected) {
            $('#dlg-bingingmeter').dialog({title:'用表绑定给业主-添加'});
            $('#metertype').val('1');
            $('#meterownerId').val(selected.OwnerId);
            $('#meterownerName').val(selected.Name);
            $('#metersrccId').val(selected.RoomId);
            $('#metersrccName').val(selected.RoomNumber);
            
            $('#cbformType').combobox('setValue','1');
            $('#cbformType').combobox('disable');
        }
    } else {
        var selected = $('#shoplistGrid').datagrid('getSelected');
        if (selected) {
            $('#dlg-bingingmeter').dialog({title:'用表绑定给业主-添加'});
            $('#metertype').val('1');
            $('#meterownerId').val(selected.OwnerId);
            $('#meterownerName').val(selected.OwnerName);
            $('#metersrccId').val(selected.ShopId);
            $('#metersrccName').val(selected.ShopName);
            
            $('#cbformType').combobox('setValue','2');
            $('#cbformType').combobox('disable');
        }
    }
    
   
}

function btn_meterdelete(){
    var selected=$('#meterlistGrid').datagrid('getSelected');
    if(selected){
            $.messager.confirm('询问', '是否要删除此用表？', function(r){
		        if (r){
                    requestAjax('../Controller.aspx?controller=SysInitController&method=DeleteMeter', { meterId: selected.MeterId}, function(data) {
                        $('#meterlistGrid').datagrid('reload');
                    });
                }
			});
    }
}

function dlg_searchmeter_confirm(){
    if(formvalidate('dlg-metersearch')==false)return false;
     $('#dlg-metersearch').dialog('close');
     var formtype=$('#cbsformType').combobox('getValue');
     var formtype2=$('#cbsformType2').combobox('getValue');
    
     $('#meterlistGrid').datagrid('options').url="../Controller.aspx?controller=SysInitController&method=GetMakerList&formtype="+formtype+"&formtype2="+formtype2;
     $('#meterlistGrid').datagrid('reload');
}

 